﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace project
{
    public partial class RentRecord : UIPage
    {
        public static Action updateDataGridView;
        public RentRecord()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(1271, 631);
            this.uiDataGridView1.Size = new System.Drawing.Size(1201, 561);
            uiDataGridView1.AllowUserToAddRows = false;
            uiDataGridView1.ReadOnly = true;
            uiDataGridView1.MultiSelect = false;
            uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        protected void QueryAllBook()
        {
            string sqlStr = "select * from rent_record";

            // 把DataGridView的数据源绑定到DataSet上的第一个表中
            uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

            // 设置表格上显示的列标题
            uiDataGridView1.Columns[0].HeaderText = "学号";
            uiDataGridView1.Columns[1].HeaderText = "书号";
            uiDataGridView1.Columns[2].HeaderText = "借阅日期";
            uiDataGridView1.Columns[3].HeaderText = "到期时间";
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {

            string str1 = string.Empty;

            switch (uiComboBox1.Text)
            {
                case "学号":
                    str1 = "student_number";
                    break;
                case "书号":
                    str1 = "book_number";
                    break;
                case "借阅日期":
                    str1 = "rent_date";
                    break;
                default:
                    str1 = "";
                    break;
            }

            if (string.IsNullOrEmpty(uiComboBox1.Text))
            {
                QueryAllBook();
            }
            else if (!string.IsNullOrEmpty(str1))
            {
                string sqlStr = $"select * from rent_record where {str1} like '%{uiTextBox1.Text}%'";

                uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

                // 设置表格上显示的列标题
            }
        }

        private void RentRecord_Load(object sender, EventArgs e)
        {

            updateDataGridView = QueryAllBook;
            QueryAllBook();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
                        //删除书
            int selectCount = uiDataGridView1.SelectedRows.Count;
            if (selectCount != 0)
            {
                int stu_id = int.Parse(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                int book_id = int.Parse(uiDataGridView1.SelectedRows[0].Cells[1].Value.ToString());
                string indate =(uiDataGridView1.SelectedRows[0].Cells[2].Value.ToString());


                int result = (int)SqlHelper.return_book(stu_id.ToString(), book_id.ToString(), indate);
                if (result != 0)
                {
                    MessageBox.Show("删除成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    QueryAllBook();
                }
                else
                {
                    MessageBox.Show("删除失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }
    }
}
